package authorize

import (
	"context"
	"fmt"
	"git.mycaigou.com/gfyx/common/jwt"
	"github.com/gin-gonic/gin"
)

const (
	TENANT_CODE_KEY  = "tenant_code"
	TENANT_NAME_KEY  = "tenant_name"
	TENANT_USER_ID   = "tenant_user_id"
	TENANT_USER_CODE = "tenant_user_code"
	TENANT_USER_NAME = "tenant_user_name"
	MOBILE_PHONE     = "mobile_phone"
	GLOBAL_USER_ID   = "global_user_id"
	LOGIN_USER_ID    = "login_user_id"
	ZhanTuUserName   = "ZhanTuUserName"
	IgnoreProfit     = "ignore_profit"
)

type AuthInfo struct {
	AppId     string `json:"app_id"`
	TimeStamp int64  `json:"time_stamp"`
}

func Authorize(ctx context.Context, sign string, authInfo *AuthInfo, appKey string) (bool, error) {
	//if ykenv.IsDevMode() == false {
	str := fmt.Sprintf("%s&%d&%s", authInfo.AppId, authInfo.TimeStamp, appKey)
	newSign, err := jwt.HmacSha256(str, appKey)
	if err != nil {
		return false, err
	}

	if sign != newSign {
		return false, nil
	}
	//}

	return true, nil
}

func GetIgnoreProfit(ctx context.Context) string {
	valueCtx := ctx.Value(IgnoreProfit)
	if value, ok := valueCtx.(string); ok {
		return value
	}
	return ""
}

func GetTenantCode(ctx context.Context) string {
	valueCtx := ctx.Value(TENANT_CODE_KEY)
	if value, ok := valueCtx.(string); ok {
		return value
	}
	return ""
}

func GetTenantName(ctx context.Context) string {
	valueCtx := ctx.Value(TENANT_NAME_KEY)
	if value, ok := valueCtx.(string); ok {
		return value
	}
	return ""
}

func GetTenantUserId(ctx context.Context) string {
	valueCtx := ctx.Value(TENANT_USER_ID)
	if value, ok := valueCtx.(string); ok {
		return value
	}
	return ""
}

func GetUserCode(ctx context.Context) string {
	valueCtx := ctx.Value(TENANT_USER_CODE)
	if value, ok := valueCtx.(string); ok {
		return value
	}
	return ""
}

func GetUserName(ctx context.Context) string {
	valueCtx := ctx.Value(TENANT_USER_NAME)
	if value, ok := valueCtx.(string); ok {
		return value
	}
	return ""
}

func GetMobilePhone(ctx context.Context) string {
	valueCtx := ctx.Value(MOBILE_PHONE)
	if value, ok := valueCtx.(string); ok {
		return value
	}
	return ""
}

func GetGlobalUserId(ctx context.Context) string {
	globalUserId := ctx.Value(GLOBAL_USER_ID)
	if value, ok := globalUserId.(string); ok {
		return value
	}
	return ""
}

func GetUserId(ctx context.Context) int64 {
	globalUserId := ctx.Value(LOGIN_USER_ID)
	if value, ok := globalUserId.(int64); ok {
		return value
	}
	return 0
}

func SetLoginContext(ctx *gin.Context, uid, tenantCode, userCode, tenantUserUid, userName, mobilePhone, tenantName string) {
	ctx.Set(GLOBAL_USER_ID, uid)
	ctx.Set(TENANT_CODE_KEY, tenantCode)
	ctx.Set(TENANT_NAME_KEY, tenantName)
	ctx.Set(TENANT_USER_CODE, userCode)
	ctx.Set(TENANT_USER_NAME, userName)
	ctx.Set(MOBILE_PHONE, mobilePhone)
	ctx.Set(TENANT_USER_ID, tenantUserUid)
}

func UnsetLoginContext(ctx *gin.Context) {
	ctx.Set(GLOBAL_USER_ID, nil)
	ctx.Set(TENANT_CODE_KEY, nil)
	ctx.Set(TENANT_NAME_KEY, nil)
	ctx.Set(TENANT_USER_ID, nil)
	ctx.Set(TENANT_USER_CODE, nil)
	ctx.Set(TENANT_USER_NAME, nil)
	ctx.Set(MOBILE_PHONE, nil)
}

func GetLoginZhanTuUserName(ctx context.Context) string {
	valueCtx := ctx.Value(ZhanTuUserName)
	if value, ok := valueCtx.(string); ok {
		return value
	}
	return ""
}
